﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Controls;
using TjZheng.Mvc;
using TjZheng.Framework;
using TjZheng.Power.DataEntity;
using TjZheng.Power.Business;

using TjZheng.Redis;
using TjZheng.Power.RedisEntity;
using TjZheng.Cache;

namespace TjZheng.CMS.Process
{
    public class CMSController : TjZhengController
    {
        /// <summary>
        /// 是否检查用户权限
        /// </summary>
        protected bool IsCheckUserPower
        {
            get
            {
                return ConfigHelper.GetBoolValue("IsCheckUserPower", true);
            }
        }
        /// <summary>
        /// 是否 从Redis检查用户权限
        /// </summary>
        protected bool IsCheckUserPowerRedis
        {
            get
            {
                return ConfigHelper.GetBoolValue("IsCheckUserPowerRedis", true);
            }
        }
        /// <summary>
        /// 跟踪参数1
        /// </summary>
        protected string TransferID { get; set; }
        /// <summary>
        /// 跟踪参数2
        /// </summary>
        protected string TransferTwoID { get; set; }
        /// <summary>
        /// 跟踪参数3
        /// </summary>
        protected string TransferThreeID { get; set; }
        /// <summary>
        /// 跟踪参数4
        /// </summary>
        protected string TransferFourID { get; set; }
        /// <summary>
        /// 查询条件组织信息
        /// </summary>
        public QueryResult QueryResult
        {
            get
            {
                return ViewBag.QueryResult as QueryResult ?? new QueryResult();
            }
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public UserInfo CurrentUser { get; private set; }

        /// <summary>
        /// 当前用户角色
        /// </summary>
        public List<GroupInfo> CurrentRoles { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CMSController()
        {
            InitialTransfers();
            InitialCurrentUser();
            //HttpContextBase在控制器构造函数中并未初始化
        }

        /// <summary>
        /// 初始化Transfer参数
        /// </summary>
        [NonAction]
        protected virtual void InitialTransfers()
        {
            TransferID = GetString("TransferID");
            TransferTwoID = GetString("TransferTwoID");
            TransferThreeID = GetString("TransferThreeID");
            TransferFourID = GetString("TransferFourID");
        }

        /// <summary>
        /// 初始化当前用户状态
        /// </summary>
        [NonAction]
        protected virtual void InitialCurrentUser()
        {
            CurrentUser = FormAuthHelper.GetUserTicket<UserInfo>();
            if (CurrentUser != null)
            {
                if (IsCheckUserPowerRedis)
                {
                    //CurrentUser = objBizUserInfo.GetByIdRedis(userId, false, PowerVariable.ProviderName, PowerVariable.Db);
                    CurrentRoles = CurrentUser.Id.CreateGroupInfoList();
                }
                else
                {
                    //CurrentUser = objBizUserInfo.GetRecord(userId);
                    //CurrentUser = objBizUserInfo.GetRecordCache(userId);
                    BizGroupInfo objBizGroupInfo = new BizGroupInfo("User");
                    //CurrentRoles = objBizGroupInfo.ConvertToGroupInfoList(objBizGroupInfo.GetViewDataTable("UserId=" + userId));
                    CurrentRoles = objBizGroupInfo.ConvertToGroupInfoList(objBizGroupInfo.GetViewDataTableCache("", "UserId=" + CurrentUser.Id));
                }
            }
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            HttpContext.SetItems("TransferID", TransferID);
            HttpContext.SetItems("TransferTwoID", TransferTwoID);
            HttpContext.SetItems("TransferThreeID", TransferThreeID);
            HttpContext.SetItems("TransferFourID", TransferFourID);
            HttpContext.SetItems("CurrentUser", CurrentUser);
            HttpContext.SetItems("CurrentRoles", CurrentRoles);
        }
    }
}
